package com.tjetc.service.impl;

import com.tjetc.vo.Result;
import com.aliyuncs.exceptions.ClientException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tjetc.domain.RankRecord;
import com.tjetc.mapper.RankRecordMapper;
import com.tjetc.util.AliYunOss;
import com.tjetc.vo.Result;
import com.tjetc.vo.Result;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.tjetc.mapper.CheckRecordMapper;
import com.tjetc.domain.CheckRecord;
import com.tjetc.service.CheckRecordService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@Service
public class CheckRecordServiceImpl implements CheckRecordService{

    @Resource
    private CheckRecordMapper checkRecordMapper;
    @Resource
    private RankRecordMapper rankRecordMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return checkRecordMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(CheckRecord record) {
        return checkRecordMapper.insert(record);
    }

    @Override
    public int insertSelective(CheckRecord record) {
        return checkRecordMapper.insertSelective(record);
    }

    @Override
    public CheckRecord selectByPrimaryKey(Integer id) {
        return checkRecordMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(CheckRecord record) {
        return checkRecordMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(CheckRecord record) {
        return checkRecordMapper.updateByPrimaryKey(record);
    }

    @Override
    public Result addJiancha(CheckRecord checkRecord) {
        int rows = checkRecordMapper.insert(checkRecord);
        return rows == 1 ? Result.ok() : Result.fail("添加失败");
    }

    @Override
    public Result getPNmaeDNameDate(String patientName, String doctorName, String startTime, String endTime, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        if (startTime.equals("")){ startTime = null;}
        if (endTime.equals("")){ endTime = null;}
       List<CheckRecord> list = checkRecordMapper.getPNmaeDNameDate(patientName,doctorName,startTime,endTime);
       return Result.ok(new PageInfo<>(list));
    }

    @Override
    @Transactional
    public Result insertOne(CheckRecord checkRecord, MultipartFile file) throws IOException, ClientException {
        byte[] bytes = file.getBytes();
        System.out.println("rank_RecordID"+checkRecord.getRankId());
        RankRecord rankRecord = rankRecordMapper.selectByPrimaryKey(checkRecord.getRankId());
        if (rankRecord== null){
            return Result.fail("未找到该预约记录");
        }else if (rankRecord.getState()==0 || rankRecord.getState()==2 || rankRecord.getState()==1){
            return Result.fail("该预约记录异常");
        }else {
            String upload = AliYunOss.upload(bytes,file.getOriginalFilename());
            checkRecord.setResultImage(upload);
            int i = checkRecordMapper.insertSelective(checkRecord);
            return i==1? Result.ok():Result.fail();
        }

    }

    @Override
    public Result updateInspect(CheckRecord checkRecord, MultipartFile file) throws IOException, ClientException {

        System.out.println("rank_RecordID"+checkRecord.getRankId());
        RankRecord rankRecord = rankRecordMapper.selectByPrimaryKey(checkRecord.getRankId());
        if (rankRecord== null){
            return Result.fail("未找到该预约记录");
        }else if (rankRecord.getState()==0 || rankRecord.getState()==2){
            return Result.fail("该预约记录异常");
        }else {
            if (file!=null){
                byte[] bytes = file.getBytes();
                String upload = AliYunOss.upload(bytes,file.getOriginalFilename());
                checkRecord.setResultImage(upload);
            }
            int i = checkRecordMapper.updateByPrimaryKeySelective(checkRecord);
            return i==1? Result.ok():Result.fail();
        }
    }


}
